//
//  MSUtils.m
//  JinliWallet
//
//  Created by 张玮 on 2018/10/17.
//  Copyright © 2018年 张玮. All rights reserved.
//

#import "MSUtils.h"

@implementation MSUtils

+ (MSUtils *)shareInstance{
    static dispatch_once_t once;
    static id sharedInstance;
    dispatch_once(&once, ^{
        sharedInstance = [[self alloc] init];
    });
    return sharedInstance;
}

- (void)showMessage:(NSString *)message{
    UIWindow *window = [UIApplication sharedApplication].delegate.window;
    //AppDelegate *app = APP_DELEGATE;
    UIView *frontView = [[window subviews] objectAtIndex:0];
    frontView.backgroundColor = [UIColor redColor];
    if(frontView){
        [frontView makeToast:message
                    duration:1.0
                    position: CSToastPositionCenter];
    }
}

- (void)showMessage:(NSString *)message time:(NSTimeInterval)time{
    UIWindow *window = [UIApplication sharedApplication].delegate.window;
    //AppDelegate *app = APP_DELEGATE;
    UIView *frontView = [[window subviews] objectAtIndex:0];
//    frontView.backgroundColor = [UIColor redColor];
    if(frontView){
        [frontView makeToast:message
                    duration:time
                    position: CSToastPositionCenter];
    }
}

-(NSString *)getUUID{
    NSString *strUUID = nil;
    strUUID = [self load:@"com.hyxnet.-cloudpc"];
    
    if(strUUID != nil || [strUUID isEqualToString:@""]){
        return strUUID;
    }else{
        CFUUIDRef uuidRef = CFUUIDCreate(kCFAllocatorDefault);
        strUUID = (NSString *)CFBridgingRelease(CFUUIDCreateString(kCFAllocatorDefault, uuidRef));
        [self save:@"com.hyxnet.-cloudpc" data:strUUID];
        return strUUID;
    }
}

- (NSMutableDictionary *)getKeychainQuery:(NSString *)service {
    return [NSMutableDictionary dictionaryWithObjectsAndKeys:
            (__bridge id)kSecClassGenericPassword, (__bridge id)kSecClass,
            service, (__bridge id)kSecAttrService,
            service, (__bridge id)kSecAttrAccount,
            (__bridge id)kSecAttrAccessibleAfterFirstUnlock, (__bridge id)kSecAttrAccessible,
            nil];
}

- (void)save:(NSString *)service data:(id)data {
    NSMutableDictionary *keychainQuery = [self getKeychainQuery:service];
    SecItemDelete((__bridge CFDictionaryRef)keychainQuery);
    [keychainQuery setObject:[NSKeyedArchiver archivedDataWithRootObject:data] forKey:(__bridge id)kSecValueData];
    SecItemAdd((__bridge CFDictionaryRef)keychainQuery, NULL);
}

-(id)load:(NSString*)service{
    id ret = nil;
    NSMutableDictionary *keychainQuery = [self getKeychainQuery:service];
    [keychainQuery setObject:(id)kCFBooleanTrue forKey:(__bridge id)kSecReturnData];
    [keychainQuery setObject:(__bridge id)kSecMatchLimitOne forKey:(__bridge id)kSecMatchLimit];
    CFDataRef keyData = NULL;
    if (SecItemCopyMatching((__bridge CFDictionaryRef)keychainQuery, (CFTypeRef *)&keyData) == noErr) {
        @try {
            ret = [NSKeyedUnarchiver unarchiveObjectWithData:(__bridge NSData *)keyData];
        }
        @catch (NSException *e) {
            NSLog(@"Unarchive of %@ failed: %@", service, e);
        }
        @finally {}
    }
    if (keyData) CFRelease(keyData);
    return ret;
}

- (void)deleteKey:(NSString *)service {
    NSMutableDictionary *keychainQuery = [self getKeychainQuery:service];
    SecItemDelete((__bridge CFDictionaryRef)keychainQuery);
}
+(void)callPhone:(NSString *)phone{
    NSMutableString *str = [[NSMutableString alloc] initWithFormat:@"telprompt://%@",phone];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str] options:@{} completionHandler:nil];
}
@end
